package miao.byusi.bkata.logs

import miao.byusi.bkata.logs.config.ConfigLoader
import miao.byusi.bkata.logs.config.LoggerConfig
import java.io.InputStream  // 添加缺失的导入
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

/**
 * Bkata 日志库主类
 */
class BkataLogger private constructor(
    private val name: String,
    private val minLevel: LogLevel,
    private val showTime: Boolean,
    private val showName: Boolean,
    private val theme: Theme
) {
    private val renderer = ConsoleRenderer(theme)
    private val timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS")
    
    /**
     * 记录调试级别日志
     */
    fun debug(message: String) = log(LogLevel.DEBUG, message)
    
    /**
     * 记录信息级别日志
     */
    fun info(message: String) = log(LogLevel.INFO, message)
    
    /**
     * 记录警告级别日志
     */
    fun warning(message: String) = log(LogLevel.WARNING, message)
    
    /**
     * 记录错误级别日志
     */
    fun error(message: String) = log(LogLevel.ERROR, message)
    
    /**
     * 记录严重级别日志
     */
    fun critical(message: String) = log(LogLevel.CRITICAL, message)
    
    /**
     * 通用日志记录方法
     */
    fun log(level: LogLevel, message: String) {
        // 检查日志级别是否满足最小级别要求
        if (level.level < minLevel.level) return
        
        // 构建日志前缀
        val prefix = buildString {
            if (showTime) {
                append("[${LocalDateTime.now().format(timeFormatter)}]")
            }
            if (showName) {
                if (isNotEmpty()) append(" ")
                append("[$name]")
            }
        }
        
        // 渲染并输出日志
        val renderedMessage = renderer.renderWithPrefix(level, prefix, message)
        println(renderedMessage)
    }
    
    companion object {
        // 加载默认配置
        private val defaultConfig = ConfigLoader.loadDefaultConfig().defaultLogger
        
        /**
         * 获取默认配置的日志记录器
         */
        fun getLogger(name: String = defaultConfig.name): BkataLogger {
            return fromConfig(name, defaultConfig)
        }
        
        /**
         * 根据配置对象创建日志记录器
         */
        fun fromConfig(name: String, config: LoggerConfig): BkataLogger {
            return BkataLogger(
                name = name,
                minLevel = LogLevel.fromString(config.minLevel),
                showTime = config.showTime,
                showName = config.showName,
                theme = config.theme.toTheme()
            )
        }
        
        /**
         * 从YAML输入流加载配置创建日志记录器
         */
        fun fromYamlConfig(inputStream: InputStream): BkataLogger {
            val config = ConfigLoader.loadConfig(inputStream).defaultLogger
            return fromConfig(config.name, config)
        }
        
        /**
         * 创建自定义配置的日志记录器
         */
        fun createCustomLogger(
            name: String = "Bkata",
            minLevel: LogLevel = LogLevel.INFO,
            showTime: Boolean = true,
            showName: Boolean = true,
            theme: Theme = Theme()
        ): BkataLogger {
            return BkataLogger(
                name = name,
                minLevel = minLevel,
                showTime = showTime,
                showName = showName,
                theme = theme
            )
        }
    }
}